package Config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Date;

import javax.naming.InitialContext;

/**
 * @author Xenophon
 * This class provide a interface to record log information
 * while the application is running
 *
 */
public class GlobalLog {
	private static File logFile;
	private static File statFile;
	private static BufferedWriter logWriter;
	private static BufferedWriter statWriter;
	
	private static void Init()
	{
		logFile = new File(Configure.LOG_PATH+"//"+Configure.LOG_FILE);
		if(!logFile.exists())
			try {
				logFile.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		statFile = new File(Configure.STAT_PATH+"//"+Configure.STAT_FILE);
		if (!statFile.exists()) {
			try {
				statFile.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static void StartLog() throws UnsupportedEncodingException, FileNotFoundException,IOException
	{
		Init();
		logWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile,true),"GB2312"));
		statWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(statFile,true),"GB2312"));
		StoreConfigure(logWriter);
		StoreConfigure(statWriter);
	}
	
	public static void CloseLog()
	{
		try{
		logWriter.close();
		statWriter.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void AddLog(String info)
	{
		try {
			logWriter.write(info+"\n");
			logWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void AddStat(String info)
	{
		try {
			statWriter.write(info+"\n");
			statWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void StoreConfigure(BufferedWriter bw) throws IOException
	{
		bw.write("==================================================\n");
		Configure.StoreConfigure(bw);
		bw.write("==================================================\n");		
	}
}
